package com.beauty.dressing.module.person;

import android.content.Intent;
import android.view.View;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.framework.common.utils.ICommonUtil;
import com.beauty.dressing.R;
import com.beauty.dressing.common.base.BaseFragment;
import com.beauty.dressing.common.bean.User;
import com.beauty.dressing.logic.control.LBController;
import com.beauty.dressing.module.mine.activity.AddressManageActivity;
import com.beauty.dressing.module.mine.activity.MyIntegralActivity;
import com.beauty.dressing.module.mine.activity.MyLeaseActivity;
import com.beauty.dressing.module.mine.activity.MyOrderActivity;
import com.beauty.dressing.module.mine.activity.OnlineServiceActivity;
import com.beauty.dressing.module.mine.activity.SettingsActivity;
import com.beauty.dressing.module.mine.activity.UserInfoActivity;
import com.beauty.dressing.rxandroid.api.ApiManager;
import com.beauty.dressing.rxandroid.rxbus.RxAction;
import com.beauty.dressing.rxandroid.rxbus.RxBus;
import com.beauty.dressing.rxandroid.rxbus.RxBusObserver;
import com.beauty.dressing.rxandroid.rxbus.RxConstants;
import com.xiaolanba.commonlib.fresco.FrescoUtil;

import java.util.Arrays;
import java.util.List;

import io.reactivex.disposables.Disposable;

/**
 * 我的
 * @author xutingz
 * @E-mail xutz@xianlanba.com
 * @date 2018/05/02
 */

public class MineFragment extends BaseFragment {
    private Disposable mDisposable;

    @Override
    public void setContentView() {
        setContentView(R.layout.fragment_mine);
    }

    @Override
    public void findView() {

//        findViewById(R.id.all_orders).setOnClickListener(this);
//        findViewById(R.id.waiting_shipped).setOnClickListener(this);
//        findViewById(R.id.waiting_receipt).setOnClickListener(this);
//        findViewById(R.id.Completed_order).setOnClickListener(this);
//        findViewById(R.id.head_iv).setOnClickListener(this);
//        findViewById(R.id.address_manage).setOnClickListener(this);
//        findViewById(R.id.In_rent).setOnClickListener(this);
//        findViewById(R.id.all_leases).setOnClickListener(this);
//        findViewById(R.id.withdrawal).setOnClickListener(this);
//        findViewById(R.id.historical_orders).setOnClickListener(this);
//        findViewById(R.id.my_integral).setOnClickListener(this);
//        findViewById(R.id.btn_setting).setOnClickListener(this);
//        findViewById(R.id.btn_online_service).setOnClickListener(this);
//        findViewById(R.id.waiting_clause).setOnClickListener(this);
        registRxBus();
    }

    @Override
    public void initData() {
        initUserInfo();
    }

    @Override
    public void onClick(View v) {
        if (ICommonUtil.isFastDoubleClick()) return;
        switch (v.getId()) {
//            case R.id.all_orders://我的全部订单
//                startActivity(new Intent(context, MyOrderActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", -1));
//                break;
//            case R.id.waiting_clause://待付款
//                startActivity(new Intent(context, MyOrderActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 0));
//                break;
//            case R.id.waiting_shipped://待发货
//                startActivity(new Intent(context, MyOrderActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 1));
//                break;
//            case R.id.waiting_receipt://待收获
//                startActivity(new Intent(context, MyOrderActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 2));
//                break;
//            case R.id.Completed_order://已完成
//                startActivity(new Intent(context, MyOrderActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 3));
//                break;
//            case R.id.head_iv://个人信息
//                startActivity(new Intent(context, UserInfoActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//                break;
//            case R.id.address_manage://地址管理
//                startActivity(new Intent(context, AddressManageActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//                break;
//            case R.id.In_rent://在租
//                startActivity(new Intent(context, MyLeaseActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 0));
//                break;
//            case R.id.all_leases://我的租赁全部订单
//                startActivity(new Intent(context, MyLeaseActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 0));
//                break;
//            case R.id.withdrawal://全部订单
//                startActivity(new Intent(context, MyLeaseActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 1));
//                break;
//            case R.id.historical_orders://全部订单
//                startActivity(new Intent(context, MyLeaseActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP).putExtra("Status", 2));
//                break;
//            case R.id.my_integral://我的积分
//                startActivity(new Intent(context, MyIntegralActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//                break;
//            case R.id.btn_setting://设置
//                startActivity(new Intent(context, SettingsActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//                break;
//            case R.id.btn_online_service://在线客服
//                startActivity(new Intent(context, OnlineServiceActivity.class).addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP));
//                break;
        }
    }

    /**
     * 通过RxBus注册监听事件
     */
    private void registRxBus() {
        List<Integer> list = Arrays.asList(RxConstants.ACTION_LOGIN_SUCCESS, RxConstants.ACTION_CHANGENICKNAME_SUCCESS);
        RxBus.getDefault().toObservableWithCodes(list).subscribeWith(new RxBusObserver<RxAction>() {
            @Override
            protected void onRxSubscribe(Disposable d) {
                mDisposable = d;
            }

            @Override
            protected void onRxNext(RxAction value) {
                switch (value.code) {
                    case RxConstants.ACTION_CHANGENICKNAME_SUCCESS://修改昵称成功的事件
                        initUserInfo();
                        break;

                }
            }
        });
    }

    private void initUserInfo() {
        User user = LBController.getInstance().getCacheManager().getLoginUser();
        if (user.image != null) {
//            Glide.with(context).load(user.image).into((ImageView) findViewById(R.id.head_iv));
            FrescoUtil.loadImage((SimpleDraweeView) findViewById(R.id.head_iv), ApiManager.HEAR_URL + user.image);
        }
        TextView username = (TextView) findViewById(R.id.username);
        if (user.userNickName != null)
            username.setText(user.userNickName);
        TextView tv_userID = (TextView) findViewById(R.id.tv_userID);
        if (user.member_id > 0) {
            tv_userID.setText("ID: " + String.valueOf(user.member_id));
        }
    }

    @Override
    public void onDestroy() {
        if (mDisposable != null) {
            mDisposable.dispose();
        }
        super.onDestroy();
    }
}
